const {mongoose, Schema} = require('./index')
const config = require('../config')

let models = {
	Token: mongoose.model('Token', new Schema({
		userId: String,
		value: String,
		createTime: {
			type: Date,
			default: Date.now
		},
		expire: {
			type: Number,
			default: config.tokenDestoryExpires
		}
	})),
	User: mongoose.model('User', new Schema({
		email: String,
		password: String,
		adminAuth: {
			type: Number,
			default: 0
		}
	})),
	Project: mongoose.model('Project', new Schema({
		userId: String,
		name: String
	})),
	Catalog: mongoose.model('Catalog', new Schema({
		projectId: String,
		userId: String,
		name: String,
		sort: {
			type:Number,
			default: 0
		}
	})),
	GlobalCode: mongoose.model('GlobalCode', new Schema({
		projectId: String,
		userId: String,
		code: Number,
		msg: String,
		description: String
	})),
	Api: mongoose.model('Api', new Schema({
		projectId: String,
		userId: String,
		navId: String,
		method: {
			type: String,
			default: 'GET'
		},
		status: String,
		url: String,
		description: String,
		resType: {
			type: String,
			default: 'application/json'
		},
		reqData: Array,
		resData: String
	})),
} 

//添加默认用户（拥有所有权限）
models.User.findOne({email:'724953302@qq.com'},(err,doc)=>{
    if(!doc){
        new models.User({
            email: '724953302@qq.com',
            password: 'admin123',
            adminAuth: 1
        }).save((err)=>{
            if(err)console.log('初始化（创建）管理员失败！--[In path:"~Server/mongodb/models.js"]', err.message)
            else console.log('成功创建管理员：724953302@qq.com[password:admin123]。--[MongoDB]')
        })
    }else if(doc.adminAuth!=1){
        models.User.update({email: '724953302@qq.com'}, {adminAuth: 1, password:'admin123'}, (err, row)=>{
            if(err) console.log('管理员授权失败！--[In path:"~Server/mongodb/models.js"]')
        })
    }
})

module.exports = models